﻿using GoStats.Model;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace GoStats.ViewModels
{
    public class SiteViewModel : INotifyPropertyChanged
    {
        private Site siteDetails;

        public Site SiteDetails
        {
            get { return siteDetails; }
            set { siteDetails = value; }
        }
        public String TodayPVs
        {
            get
            {
                if (siteDetails.TodayStats != null)
                    return siteDetails.TodayStats.pageViews + " pageviews";
                else return "";
            }
        }
        public String TodayVs
        {
            get
            {
                if (siteDetails.TodayStats != null)
                    return siteDetails.TodayStats.uniqueVisitors + " visitors";
                else return "";
            }
        }

        public String YesterdayPVs
        {
            get
            {
                if (siteDetails.YesterdayStats != null)
                    return siteDetails.YesterdayStats.pageViews + " pageviews";
                else return "";
            }
        }
        public String YesterdayVs
        {
            get
            {
                if (siteDetails.YesterdayStats != null)
                    return siteDetails.YesterdayStats.uniqueVisitors + " visitors";
                else return "";
            }
        }

        public String SevenPVs
        {
            get
            {
                if (siteDetails.SevenStats != null)
                    return siteDetails.SevenStats.pageViews + " pageviews";
                else return "";
            }
        }
        public String SevenVs
        {
            get
            {
                if (siteDetails.SevenStats != null)
                    return siteDetails.SevenStats.uniqueVisitors + " visitors";
                else return "";
            }
        }


        public String ThirtyPVs
        {
            get
            {
                if (siteDetails.ThirtyStats != null)
                    return siteDetails.ThirtyStats.pageViews + " pageviews";
                else return "";
            }
        }
        public String ThirtyVs
        {
            get
            {
                if (siteDetails.ThirtyStats != null)
                    return siteDetails.ThirtyStats.uniqueVisitors + " visitors";
                else return "";
            }
        }

        private Boolean isAvailable;

        public Boolean IsAvailable
        {
            get { return isAvailable; }
            set {
                isAvailable = value;
                NotifyPropertyChanged("IsAvailable");
                NotifyPropertyChanged("SiteDetails");
                NotifyPropertyChanged("TodayPVs");
                NotifyPropertyChanged("TodayVs");
                NotifyPropertyChanged("YesterdayPVs");
                NotifyPropertyChanged("YesterdayVs");
                NotifyPropertyChanged("SevenPVs");
                NotifyPropertyChanged("SevenVs");
                NotifyPropertyChanged("ThirtyPVs");
                NotifyPropertyChanged("ThirtyVs");
            }
        }

        private Boolean isLoading;

        public Boolean IsLoading
        {
            get { return isLoading; }
            set
            {
                isLoading = value;
                NotifyPropertyChanged("IsLoading");
            }
        }

        private Boolean isFailed;

        public Boolean IsFailed
        {
            get { return isFailed; }
            set
            {
                isFailed = value;
                NotifyPropertyChanged("IsFailed");
            }
        }


        public async void load()
        {
            IsLoading = true;
            IsAvailable = false;
            IsFailed = false;
            Boolean success = await API.details(SiteDetails);
            if (success)
            {
                IsAvailable = true;
                IsLoading = false;
                IsFailed = false;
            }
            else
            {
                IsAvailable = false;
                IsLoading = false;
                IsFailed = true;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}